package cn.wsh.basic.controller;

import cn.wsh.basic.dto.LoginDto;
import cn.wsh.basic.exception.BusinessException;
import cn.wsh.basic.service.ILoginService;
import cn.wsh.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/login")

public class LoginController {
    @Autowired
    private ILoginService loginService;

    /**
     * 账号密码登录
     *
     * @param dto 需要传入type  0 代表后端  1 代表前端
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto) {
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }

    }

    /**
     * 微信登录接口 code 授权码
     */

    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String, String> param) {
        try {
            return loginService.wechat(param);
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }

    }

    /**
     * 微信绑定接口
     * @param param
     * @return
     */

    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String, String> param) {
        try {
            Map<String, Object> map = loginService.binder(param);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }

    }


}
